﻿<Window x:Class="SharePointLogViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:SharePointLogViewer"
    xmlns:controls="clr-namespace:SharePointLogViewer.Controls"
    xmlns:actextbox="clr-namespace:SharePointLogViewer.Controls.AutoCompleteTextBox"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SharePoint LogViewer" 
    Height="{local:SettingBinding Height}"
    Width="{local:SettingBinding Width}"
    Left="{local:SettingBinding Left}"
    Top="{local:SettingBinding Top}"
    Icon="/SharePointLogViewer;component/Images/SPLV.ico" Closing="Window_Closing" StateChanged="Window_StateChanged" >
    <Window.Resources>
        <local:HiglightBookmarkedValueConverter x:Key="BookmarkedAndNotSelected"/>
        <CollectionViewSource x:Key="FilteredCollection" Source="{Binding}"/>
        <DataTemplate x:Key="BookmarkColumnTemplate">
            <Image x:Name="img" MouseLeftButtonUp="img_MouseLeftButtonUp" ToolTip="Bookmark" 
                       Cursor="Hand" Source="Images\empty.png">
            </Image>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Bookmarked}" Value="True">
                    <Setter TargetName="img" Property="Image.Source" Value="Images\bookmark.png"/>
                    <Setter TargetName="img" Property="Image.ToolTip" Value="Delete Bookmark"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <Style x:Key="LogEntryStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource AlternatingListViewItemStyle}">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Value="True">
                            <Condition.Binding>
                                <MultiBinding Converter="{StaticResource BookmarkedAndNotSelected}">
                                    <Binding RelativeSource="{RelativeSource self}" Path="DataContext.Bookmarked" />
                                    <Binding Path="IsSelected" RelativeSource="{RelativeSource self}" />
                                </MultiBinding>
                            </Condition.Binding>
                        </Condition>
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter Property="Background" Value="#FFA6BBDC" />
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="local:MainWindow.Settings" Executed="Settings_Executed" />
        <CommandBinding Command="local:MainWindow.About" Executed="About_Executed" />
        <CommandBinding Command="local:MainWindow.Filter" Executed="Filter_Executed" />
        <CommandBinding Command="local:MainWindow.Refresh" CanExecute="Refresh_CanExecute" Executed="Refresh_Executed" />
        <CommandBinding Command="local:MainWindow.OpenFile" CanExecute="OpenFile_CanExecute" Executed="OpenFile_Executed" />
        <CommandBinding Command="local:MainWindow.ToggleLiveMonitoring" Executed="LiveMode_Executed"/>
        <CommandBinding Command="local:MainWindow.ExportLogEntries" Executed="ExportLogEntries_Executed"/>
        <CommandBinding Command="local:MainWindow.Previous" CanExecute="Navigate_CanExecute" Executed="Previous_Executed"/>
        <CommandBinding Command="local:MainWindow.Next" CanExecute="Navigate_CanExecute" Executed="Next_Executed"/>
        <CommandBinding Command="local:MainWindow.ToggleBookmark" CanExecute="ToogleBookmark_CanExecute" Executed="ToggleBookmark_Executed"/>
        <CommandBinding Command="local:MainWindow.ClearLogs" CanExecute="ClearLogs_CanExecute" Executed="ClearLogs_Executed"/>
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Command="local:MainWindow.About" Key="F1"/>
        <KeyBinding Command="local:MainWindow.Filter" Key="F3"/>
        <KeyBinding Command="local:MainWindow.Refresh" Key="F5"/>
        <KeyBinding Command="local:MainWindow.OpenFile" Key="O" Modifiers="Ctrl"/>
        <KeyBinding Command="local:MainWindow.ToggleLiveMonitoring" Key="F5" Modifiers="Ctrl"/>
        <KeyBinding Command="local:MainWindow.ExportLogEntries" Key="S" Modifiers="Ctrl"/>
        <KeyBinding Command="local:MainWindow.Previous" Key="P" Modifiers="Ctrl" />
        <KeyBinding Command="local:MainWindow.Next" Key="N" Modifiers="Ctrl" />
        <KeyBinding Command="local:MainWindow.ToggleBookmark" Key="B" Modifiers="Ctrl" />
    </Window.InputBindings>

    <Grid AllowDrop="True" Drop="Grid_Drop">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="30" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0" Background="{StaticResource ResourceKey=BlueGradient}">
            <StackPanel Height="30" Orientation="Horizontal" DockPanel.Dock="Left">
                <Button IsTabStop="False" Style="{StaticResource GlassButton}" Height="30"
                        Name="btnBrowse"
                        Width="30" ToolTip="Open File(s) (Ctrl + O)" Command="local:MainWindow.OpenFile"
                        Background="SteelBlue">                    
                    <Image Source="Images\openfile.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button IsTabStop="False" Style="{StaticResource GlassButton}" Height="30"  Margin="10,0,0,0" 
                        Name="btnToggleLive" Width="30" Tag="Images/play.png"
                        ToolTip="Start Live Monitoring. (Ctrl + F5)" Command="local:MainWindow.ToggleLiveMonitoring"
                        Background="SteelBlue">
                    <Image x:Name="imgPlay" Style="{StaticResource ToolBarImageStyle}" Source="{Binding ElementName=btnToggleLive, Path=Tag}" Stretch="None" />
                </Button>
                <Button IsTabStop="False" Style="{StaticResource GlassButton}" Height="30"  Margin="10,0,0,0" 
                        Name="btnExport" Width="30" Foreground="White" FontSize="8"
                        ToolTip="Export Log Entries (Ctrl + S)" Command="local:MainWindow.ExportLogEntries"
                        Background="SteelBlue">
                    <Image Source="Images/export.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Separator/>
                <Label Margin="10,0,0,0" Content="Filter: " VerticalAlignment="Center" />
                <ComboBox x:Name="cmbFilterBy" SelectedIndex="0" Width="100" Height="23"
                          VerticalContentAlignment="Center" Background="{StaticResource ResourceKey=BlueGradient}">                    
                    <ComboBoxItem>Any field</ComboBoxItem>
                    <ComboBoxItem>Timestamp</ComboBoxItem>
                    <ComboBoxItem>Process</ComboBoxItem>
                    <ComboBoxItem>TID</ComboBoxItem>
                    <ComboBoxItem>Area</ComboBoxItem>
                    <ComboBoxItem>Category</ComboBoxItem>
                    <ComboBoxItem>EventID</ComboBoxItem>
                    <ComboBoxItem>Level</ComboBoxItem>
                    <ComboBoxItem>Message</ComboBoxItem>
                    <ComboBoxItem>Correlation</ComboBoxItem>
                </ComboBox>                
                <actextbox:AutoCompleteTextBox x:Name="txtFilter" Height="23" Width="300"
                         Margin="5,0,5,0"  VerticalContentAlignment="Center" />
                <Button Style="{StaticResource GlassButton}" Height="30" Width="30" 
                        Name="btnSearch"
                        ToolTip="Search (F3)" Background="SteelBlue"
                        Command="local:MainWindow.Filter" IsDefault="True">
                    <Image Source="Images\search.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button Style="{StaticResource GlassButton}" Margin="10,0,0,0"  Height="30" Width="30"
                        Name="btnRefresh"
                        ToolTip="Reload (F5)" Background="SteelBlue"
                        Command="local:MainWindow.Refresh" >
                    <Image Source="Images\refresh.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button Style="{StaticResource GlassButton}" Margin="10,0,0,0"  Height="30" Width="30"
                        Name="btnClear"
                        ToolTip="Clear All Logs" Background="SteelBlue"
                        Command="local:MainWindow.ClearLogs" >
                    <Image Source="Images\clearall.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button Style="{StaticResource GlassButton}" Margin="10,0,0,0" Height="30" Width="30"
                        ToolTip="Toggle a bookmark on selected entry (Ctrl+B)" Background="SteelBlue"
                        Command="local:MainWindow.ToggleBookmark" >
                    <Image Source="Images\bookmark.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button Style="{StaticResource GlassButton}" Margin="10,0,0,0" Height="30" Width="30"
                        ToolTip="Previous bookmark (Ctrl+P)" Background="SteelBlue"
                        Command="local:MainWindow.Previous" >
                    <Image Source="Images\previous.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button Style="{StaticResource GlassButton}" Margin="10,0,0,0"  Height="30" Width="30"
                        ToolTip="Next bookmark (Ctrl+N)" Background="SteelBlue"
                        Command="local:MainWindow.Next" >
                    <Image Source="Images\next.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
                <Button Style="{StaticResource GlassButton}" Margin="10,0,0,0" Height="30" Width="30"
                    ToolTip="Settings"
                    DockPanel.Dock="Right" HorizontalAlignment="Right" Command="local:MainWindow.Settings" 
                    Background="SteelBlue" HorizontalContentAlignment="Right">
                    <Image Source="Images\settings.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
                </Button>
            </StackPanel>
            <Button Style="{StaticResource GlassButton}" Height="30" Width="30"  Margin="10,0,0,0" 
                    ToolTip="About SharePoint LogViewer (F1)"
                    DockPanel.Dock="Right" HorizontalAlignment="Right" Command="local:MainWindow.About" 
                    Background="SteelBlue" HorizontalContentAlignment="Right">
                <Image Source="Images\about.png" Style="{StaticResource ToolBarImageStyle}" Stretch="None" />
            </Button>
        </DockPanel>
        <local:LogFilterListView Grid.Row="1" ItemContainerStyle="{StaticResource LogEntryStyle}" 
                                ItemsSource="{Binding Source={StaticResource FilteredCollection}}"
                                FilterButtonActiveStyle="{StaticResource filterButtonActive}"           
                                FilterButtonInactiveStyle="{StaticResource filterButtonInactive}" 
                                AlternationCount="2" x:Name="lstLog">           
            <ListView.CommandBindings>
                <CommandBinding Command="Copy" Executed="lvCopyCommand_Executed"/>
            </ListView.CommandBindings>
            <local:LogFilterListView.View>
                <GridView x:Name="grdLog" >
                    <controls:SortableGridViewColumn x:Name="hdrBookmark" SortPropertyName="Bookmarked" CanBeFiltered="False" CellTemplate="{StaticResource BookmarkColumnTemplate}" />
                    <controls:SortableGridViewColumn x:Name="hdrTimestamp" Header="Timestamp" DisplayMemberBinding="{Binding Timestamp}" CanBeFiltered="False" SortPropertyName="Timestamp" />
                    <controls:SortableGridViewColumn x:Name="hdrProcess" Header="Process" DisplayMemberBinding="{Binding Process}" CanBeFiltered="True" SortPropertyName="Process"/>
                    <controls:SortableGridViewColumn x:Name="hdrTid" Header="TID" DisplayMemberBinding="{Binding TID}" CanBeFiltered="False" SortPropertyName="TID"/>
                    <controls:SortableGridViewColumn x:Name="hdrArea" Header="Area" DisplayMemberBinding="{Binding Area}" CanBeFiltered="True" SortPropertyName="Area"/>
                    <controls:SortableGridViewColumn x:Name="hdrCategory" Header="Category" DisplayMemberBinding="{Binding Category}" CanBeFiltered="True" SortPropertyName="Category"/>
                    <controls:SortableGridViewColumn x:Name="hdrEventID" Header="EventID" DisplayMemberBinding="{Binding EventID}" CanBeFiltered="False" SortPropertyName="EventID"/>
                    <controls:SortableGridViewColumn x:Name="hdrLevel" Header="Level" DisplayMemberBinding="{Binding Level}" CanBeFiltered="True" SortPropertyName="Level"/>
                    <controls:SortableGridViewColumn x:Name="hdrMessage" Header="Message" DisplayMemberBinding="{Binding Message}" CanBeFiltered="False" SortPropertyName="Message"/>
                    <controls:SortableGridViewColumn x:Name="hdrCorrelation" Header="Correlation" DisplayMemberBinding="{Binding Correlation}" CanBeFiltered="False" SortPropertyName="Correlation"/>
                </GridView>
            </local:LogFilterListView.View>
        </local:LogFilterListView>
        <Expander Grid.Row="2" HorizontalAlignment="Stretch" Header="Message" IsExpanded="True"
                  VerticalAlignment="Top" ExpandDirection="Down" Padding="2" Background="{StaticResource PressedBrush}">
                    <TextBox Height="100" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" IsReadOnly="True"
                         Text="{Binding ElementName=lstLog, Path=SelectedValue.Message, Mode=OneWay}" />
        </Expander>

        <Border x:Name="bdrShadow" Grid.RowSpan="3" Background="WhiteSmoke" Opacity="0.50" Visibility="Hidden" >
            <controls:LoadingAnimation x:Name="loadingAnimation" Grid.RowSpan="3"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>
        <Border x:Name="bdrAbout" Grid.Row="1" Grid.RowSpan="2" Background="WhiteSmoke" Opacity="0.90" Visibility="Hidden">
            <TextBlock FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center">
                SharePoint Log Viewer <TextBlock x:Name="lblVersion" /><LineBreak />
                Copyright(C) 2010 Overroot Inc.<LineBreak /><LineBreak />
                Website: <Hyperlink NavigateUri="http://www.overroot.com" RequestNavigate="Hyperlink_RequestNavigate">www.overroot.com</Hyperlink><LineBreak />
                Email: <Hyperlink NavigateUri="mailto:info@overroot.com" RequestNavigate="Hyperlink_RequestNavigate">info@overroot.com</Hyperlink>
            </TextBlock>
        </Border>
    </Grid>
</Window>
